package net.sxjr.protal.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.sxjr.common.Response;
import net.sxjr.mbg.entity.IgSmsManage;
import net.sxjr.protal.mapper.LoginMapper;
import net.sxjr.protal.mapper.SmsManageMapper;
import net.sxjr.protal.user.dao.LoginUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：cuixc
 * @date ：Created in 2020/12/11 16:06
 * @description：
 * @modified By：
 */
@Service
public class SmsManageService {
    @Autowired
    private SmsManageMapper smsManageMapper;
    @Autowired
    private LoginMapper loginMapper;

    public Response list(Integer size, Integer current,String accounrt) {
        Page<IgSmsManage> page = new Page<>(current,size);
        Page<IgSmsManage> smsList = smsManageMapper.getSmsList(page, accounrt);
        for (IgSmsManage smsManage : smsList.getRecords()) {
            String remindLink = smsManage.getRemindLink();
            String nameByLinkCode = loginMapper.getNameByLinkCode(remindLink);
            smsManage.setRemindLink(nameByLinkCode);
        }

        return Response.success(smsList);
    }

    //阅读消息
    public Response read(String smsId) {
        IgSmsManage smsManage = smsManageMapper.selectById(smsId);
        smsManage.setIsRead(1);
        smsManage.updateById();
        return Response.success(smsManage);
    }

    public Response readSmsFromLinkCode(String linkCode, String superviseCode) {
        List<IgSmsManage> igSmsManages = smsManageMapper.selectList(new LambdaQueryWrapper<IgSmsManage>().eq(IgSmsManage::getRemindLink, linkCode).eq(IgSmsManage::getSuperviseCode, superviseCode));
        if(igSmsManages != null && igSmsManages.size()>0){
            IgSmsManage smsManage = igSmsManages.get(0);
            String remindLink = smsManage.getRemindLink();
            String nameByLinkCode = loginMapper.getNameByLinkCode(remindLink);
            smsManage.setRemindLink(nameByLinkCode);
            return Response.success(smsManage);
        }
        return Response.success();

    }
}
